package com.haoxuewa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoxuewa.dao.UserDao;
import com.haoxuewa.dao.VisitQuantityDao;
import com.haoxuewa.entity.Essay;
import com.haoxuewa.entity.EssayTemp;
import com.haoxuewa.entity.User;
import com.haoxuewa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    private final UserDao userDao;

    @Autowired
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public Page<User> selectAllUser(int pageNum, int pageSize) {
        return userDao.selectMapsPage(new Page(pageNum, pageSize), new QueryWrapper<User>()
                .select("LPAD(id,9,0) id", "name", "ifnull(birthday,'未知') birthday", "ifnull(gender,'未知') gender"));
    }

    @Override
    public List<Map<String, Object>> userGenderQuantity() {
        return userDao.selectMaps(new QueryWrapper<User>()
                .select("", "count(*) quantity").groupBy("gender"));
    }

    @Override
    public Boolean deleteUser(String name) {
        return userDao.delete(new QueryWrapper<User>().eq("name", name)) == 1;
    }

    @Override
    public Boolean addUser(String userName, String password, String gender, String birthday) {
        User user = new User();
        user.setName(userName);
        user.setPassword(password);
        user.setGender(gender);
        user.setBirthday(birthday);
        return userDao.insert(user) == 1;
    }
}
